set(SRCS ./ZWayEsp32Wifi.c ./ZWayEsp32Ethernet.c ./ZWayEsp32Webserver.c ./main.c)
if(NOT SRCS)
	set(SRCS "")
endif()

set(INCLUDE_DIRS )
if(NOT INCLUDE_DIRS)
	set(INCLUDE_DIRS "")
endif()

set(PRIV_REQUIRES )
if(NOT PRIV_REQUIRES)
	set(PRIV_REQUIRES "")
endif()

set(EMBED_FILES ./wui.tar)
if(NOT EMBED_FILES)
	set(EMBED_FILES "")
endif()

foreach(file ${EMBED_FILES})
	get_filename_component(name ${file} NAME)
	list(APPEND SRCS "${CMAKE_CURRENT_BINARY_DIR}/embed_files/${name}_add.c")
endforeach()

list(APPEND SRCS "${CMAKE_CURRENT_BINARY_DIR}/embed_files/embed_generate.cmake")

idf_component_register(
	SRCS "${SRCS}"
	INCLUDE_DIRS "${INCLUDE_DIRS}"
	PRIV_REQUIRES "${PRIV_REQUIRES}"
	EMBED_FILES "${EMBED_FILES}"
)

add_custom_command(
	OUTPUT "${CMAKE_CURRENT_BINARY_DIR}/embed_files/embed_generate.cmake"
	COMMAND ${CMAKE_COMMAND} -E make_directory "${CMAKE_CURRENT_BINARY_DIR}/embed_files"
	COMMAND ${CMAKE_COMMAND} -E echo "cmake_minimum_required(VERSION \"3.16.0\")" > "${CMAKE_CURRENT_BINARY_DIR}/embed_files/embed_generate.cmake"
	COMMAND ${CMAKE_COMMAND} -E echo "string(TIMESTAMP output_variable \"%a, %d %b %Y %H:%M:%S GMT\" UTC)" >> "${CMAKE_CURRENT_BINARY_DIR}/embed_files/embed_generate.cmake"
	COMMAND ${CMAKE_COMMAND} -E echo "string(MAKE_C_IDENTIFIER \"\${NAME}\" name)" >> "${CMAKE_CURRENT_BINARY_DIR}/embed_files/embed_generate.cmake"
	COMMAND ${CMAKE_COMMAND} -E echo "file(WRITE \${EMBED_FILE_OUT} \"const char g_\${name}_last_modified[] = \\\"\${output_variable}\\\";\")" >> "${CMAKE_CURRENT_BINARY_DIR}/embed_files/embed_generate.cmake"
	VERBATIM
)

foreach(file ${EMBED_FILES})
	get_filename_component(name ${file} NAME)
	add_custom_command(
		OUTPUT "${CMAKE_CURRENT_BINARY_DIR}/embed_files/${name}_add.c"
		COMMAND ${CMAKE_COMMAND} -E make_directory "${CMAKE_CURRENT_BINARY_DIR}/embed_files"
		COMMAND ${CMAKE_COMMAND} -DNAME=${name} -DEMBED_FILE_OUT=embed_files/${name}_add.c -P "${CMAKE_CURRENT_BINARY_DIR}/embed_files/embed_generate.cmake"
		DEPENDS ${file} "${CMAKE_CURRENT_BINARY_DIR}/embed_files/embed_generate.cmake"
		VERBATIM
	)
endforeach()

set(DEFINITIONS_COMMON __ESP32__ __FREERTOS__ __CRYPTO_MBEDTLS__ __ZWAY_ZDDX_DB__=0 __ZWAY_STATIC_CONFIGURATION__ __ZWAY_COLORIZED_LOGGING__ __ZWAY_SAVE_CBOR__ __ZWAY_SAVE_SEPARATE_DEVICE_FILES__ __ZWAY_STORAGE_VIA_FILE__ __ZWAY_CLI_DESCRIPTION_DISABLE__ SPAN_TABLE_SIZE=10 WORKBUF_SIZE=200)
if(DEFINITIONS_COMMON)
	idf_build_set_property(COMPILE_DEFINITIONS "${DEFINITIONS_COMMON}" APPEND)
endif()

if(EXISTS ${CMAKE_CURRENT_LIST_DIR}/../cfg/project_config_define.cmake)
	include(${CMAKE_CURRENT_LIST_DIR}/../cfg/project_config_define.cmake)
	if(PROJECT_CONFIG_DEFINE)
		target_compile_definitions(${COMPONENT_LIB} PRIVATE "${PROJECT_CONFIG_DEFINE}")
	endif()
endif()

# for https://github.com/espressif/esp-idf/issues/15103
set(FIXATION_FUNCTION esp_littlefs_info uart_vfs_dev_use_nonblocking)
if(FIXATION_FUNCTION)
	foreach(x ${FIXATION_FUNCTION})
		target_link_libraries(${COMPONENT_LIB} PRIVATE "-Wl,-u ${x}")
	endforeach()
endif()



